# -*- coding: utf-8 -*-
# @Time    : 2020/10/22 13:32
# @Author  : Leo
# @FileName: user_manage.py
# @Software: PyCharm
from models.user.user_model import UserModel
from utils.time_utils import is_right_time_to_order

class UserManager:
    def get_by_userId(self, user_id):
        '''
        根据user_id获取用户信息
        @param user_id:
        @return:
        '''
        return UserModel().get_dataByID(user_id)


    def get_by_loginName(self, loginName):
        '''
        根据loginName获取用户信息
        @param loginName:
        @return:
        '''
        return UserModel().get_dataByName(loginName)


    def is_exists_by_userId(self, user_id):
        '''
        根据用户id判断用户是否存在
        @param user_id:
        @return:
        '''
        user_info = self.get_by_userId(user_id)
        if not user_info:
            return False
        return True


    def is_exists_by_loginName(self, loginName):
        '''
        根据登录名判断用户是否存在
        @param loginName:
        @return: True or False
        '''
        user_info = self.get_by_loginName(loginName)
        if not user_info:
            return False
        return True

    def login(self, loginName, password):
        '''
        用户登录
        @param loginName:
        @param password:
        @return: 登录成功或登录失败信息
        '''
        # 用户不存在
        if not self.is_exists_by_loginName(loginName):
            return {"errorCode": 401, "message":"用户不存在"}

        # 验证密码是否匹配
        user_info = self.get_by_loginName(loginName)[0]

        # 密码不匹配
        if user_info['pwd'] != password:
            return {"errorCode": 402,  "message":"密码错误"}
        # 登录成功，返回userId
        return {"userId": user_info['user_id']}
    def list(self):
        '''
        获取所有用户信息
        @return:
        '''
        return UserModel().get_allData()

    def update(self, user_info):
        '''
        更改用户信息
        @param user_info:
        @return:
        '''
        # 微信绑定
        if user_info['wechat_id']:
            UserModel().update_userDataByName(name=user_info['name'], weChartID=user_info['wechat_id'])

    def send_wechat_email(self, userId):
        '''
        发送微信绑定邮件
        '''
        if not self.is_exists_by_userId(userId):
            return {'code': 401,
                    'message': "用户不存在"}
        user_info = self.get_by_userId(userId)[0]
        # 检查smtp配置

        # 检查邮箱是否存在
        if not user_info['email']:
                return {'code':402,
                        'message':"用户邮箱不存在"}

        from utils.send_email import WechatQREmail
        WechatQREmail().sendMail(
                            to_addr=user_info["email"],
                            userId=userId)

    ############下单和取消订单##############
    '''
     name,
            isOrder=0,
            orderTime=strftime(
                "%Y-%m-%d %H:%M:%S",
                localtime()),
            position="sh",
            orderType="Launch"
    '''
    def update_order(self, loginName, isOrder, position="sh", orderType="Launch"):
        UserModel().update_orderDataByName(name=loginName, isOrder=isOrder, position=position, orderType=orderType)
        return {"success": "success"}
    #######结束###########################

    def get_order(self, loginName):
        isOrder = 0
        for i in UserModel().get_dataByName(name=loginName):
            isOrder = i["isOrder"]
            OrderType = i["orderType"]
        return {"isOrder": isOrder, "orderTpye": OrderType}